package com.hnou.boxuegu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hnou.boxuegu.R;
import com.hnou.boxuegu.activity.CourseDetailActivity;
import com.hnou.boxuegu.bean.CourseBean;

import java.util.List;

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.MyViewHolder> {
    private Context mContext;
    private List<CourseBean> cbl;
    public CourseAdapter(Context context){
        this.mContext = context;
    }
    public void setDate(List<CourseBean> cbl){
        this.cbl = cbl;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.course_list_item,null);
        MyViewHolder holder = new MyViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder( MyViewHolder holder, int position) {
        final  CourseBean bean = cbl.get(position);
        holder.tv_title.setText(bean.getChapterName());
        Glide
                .with(mContext)
                .load(bean.getChapterImg())
                .error(R.mipmap.ic_launcher)
                .into(holder.iv_img);
        holder.iv_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到课程详情界面
                Intent intent = new Intent(mContext, CourseDetailActivity.class);
                intent.putExtra("CourseBean",bean);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return cbl == null ? 0 :cbl.size();
    }

    public class MyViewHolder  extends RecyclerView.ViewHolder{
        TextView tv_title;
        ImageView iv_img;
        public MyViewHolder( View view) {
            super(view);
            iv_img = view.findViewById(R.id.iv_img);
            tv_title = view.findViewById(R.id.tv_title);
        }
    }
}
